import 'package:flutter/material.dart';
import 'package:flutter_app/common/smooth_scroll.dart';
import 'package:flutter_app/views/todo_section.dart';
import 'package:get/get.dart';
import '../controller/list.dart';

class MyHome extends StatelessWidget {
  final todoController = Get.put(TodoController());
  int i = 0;
  Map<String, Widget> memo = {};

  MyHome({Key? key}) : super(key: key) {
    for (var j = 0; j < 10; j++) {
      todoController.add(Todo((i++).toString(), oneOf(sentenceList)));
    }
  }

  @override
  Widget build(context) {
    return Scaffold(
        // Use Obx(()=> to update Text() whenever count is changed.
        appBar: AppBar(title: const Text("托青鸟寄给茫茫人海")),

        // Replace the 8 lines Navigator.push by a simple Get.to(). You don't need context
        body: SmoothScrollWeb(
          builder: smoothListBuilder,
        ),
        floatingActionButton: FloatingActionButton(
            child: const Icon(Icons.add),
            onPressed: () {
              todoController.add(Todo((i++).toString(), oneOf(sentenceList)));
            }));
  }

  Widget smoothListBuilder(BuildContext context, SmoothArgs args) {
    return Obx(() => ListView(
          controller: args.controller,
          physics: args.physics,
          children: todoController.todos.map((todo) {
            final section = memo[todo.title] ??
                TodoSection(
                  todo,
                  key: ObjectKey(todo.title),
                );
            memo[todo.title] = section;
            return section;
          }).toList(),
        ));
  }
}
